import config from "@/ajax/defaultConfig";
/**
 * @function isEmpty 判断值是否为空
 */
const isEmpty = (data: any): boolean => {
  return (
    data === undefined ||
    data === null ||
    (typeof data === "object" && Object.keys(data).length === 0) ||
    (typeof data === "string" && data.trim().length === 0)
  );
};
/**
 * @name isApp 是否在菱菱邦app内
 * @returns boolean
 */
const isApp = (): boolean => {
  return false;
};
export { isEmpty };
/*
 * 获取租户id
 */
export const getBaseTenant = function () {
  return "781452594638753792";
};
/*
 * 获取基本的请求header
 * 有token传token没有就取tenant
 */
export const getBaseHeader = function () {
  const header: any = {};
  const value = "";
  if (value) {
    header.Authorization = "Bearer " + value;
  } else {
    header.tenant = getBaseTenant();
  }
  return header;
};
/*
 * 获取基本的请求header
 * 有token传token没有就取tenant
 */
export const getBasePreviewUrl = function (name: string):string {
  let data = {
    ...getBaseHeader(),
    name,
  };
  return config.baseURL + "api/file/preview" + joinUrl(data);
};
/*
 * 获取基本的请求header
 * 有token传token没有就取tenant
 */

const joinUrl = function (obj: AnyObj, encode?: boolean) {
  let url = "";
  for (let key in obj) {
    // 转码涉及很多问题，暂时不转
    // url += '&' + key + '=' + (encode?encodeURIComponent(obj[key]):obj[key])
    url += "&" + key + "=" + (obj[key] || obj[key] === 0 ? obj[key] : "");
  }
  url = url.replace("&", "?");
  return url;
};
